//Properties的用法,给出案例

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

public class PropertiesExample {
    public static void main(String[] args) {
        // 创建 Properties 对象
        Properties properties = new Properties();

        // 加载配置文件
        try (FileInputStream input = new FileInputStream("config.properties")) {
            properties.load(input);
            // 读取属性
            String dbUrl = properties.getProperty("db.url");
            String dbUser = properties.getProperty("db.user");
            String dbPassword = properties.getProperty("db.password");

            System.out.println("数据库URL: " + dbUrl);
            System.out.println("数据库用户: " + dbUser);
            System.out.println("数据库密码: " + dbPassword);
        } catch (IOException e) {
            e.printStackTrace();
        }

        // 修改属性
        properties.setProperty("db.password", "newpassword123");

        // 保存配置文件
        try (FileOutputStream output = new FileOutputStream("config.properties")) {
            properties.store(output, "Updated database password");
            System.out.println("配置文件已更新。");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
